﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Text;

namespace CondeNet.AssetManager.DataProvider.Sql
{
    public static class DataContextFactory
    {
        private static readonly string _connectionString;
        private static readonly MappingSource _mappingSource;

        static DataContextFactory()
        {
            try
            {
                // set the connection string as the configured/selected in app.config
                string connectionStringName = ConfigurationManager.AppSettings["ConnectionStringName"];
                _connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;

                DataContext context = new AssetManagerDataContext(_connectionString);
                _mappingSource = context.Mapping.MappingSource;
            }
            catch (Exception ex)
            {
                // fatal error
            }
        }

        public static AssetManagerDataContext CreateContext()
        {
            return new AssetManagerDataContext(_connectionString);
        }
    }
}
